import java.util.Random;
/**
 *
 * @author Alejandra Samayoa
 * 
 */
public class Arrey {
    private int[] datos;
    private int size;
    public Arrey(int num){
        size = num;
        datos = new int[num];
    }
    
    public void Fill(){
        int i;
        Random generator = new Random();
        for (i=0;i<size;i++)
        datos[i] = generator.nextInt(5000);
    }
    
    private static int partition(int data[], int left, int right)
    // pre: left <= right
    // post: data[left] placed in the correct (returned) location
    {
       while (true)
        {
        // move right "pointer" toward left
        while (left < right && data[left] < data[right]) right--;
        if (left < right) swap(data,left++,right);
        else return left;
        // move left pointer toward right
        while (left < right && data[left] < data[right]) left++;
        if (left < right) swap(data,left,right--);
        else return right;
        }
    }
    private static void swap(int data[], int i, int j)
    // pre: 0 <= i,j < data.length
    // post: data[i] and data[j] are exchanged
    {
        int temp; 
        temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }   

    private static void quickSortR(int data[], int left, int right)
    // pre: left<= right
    // post data[left..right] en orden ascendente
    {
        int pivote;
        if (left>=right) return;
        pivote = partition(data,left,right); /* 1 - place pivot */
        quickSortR(data,left,pivote-1); /* 2 - sort small */
        quickSortR(data,pivote+1,right);/* 3 - sort large */
    }
    
    public void QuickSort(){
        quickSortR(datos,0,size-1);
    }
    
    public void SelectionSort(){
        int numUnsorted = size;
        int index; // general index
        int max; // index of largest value
        while (numUnsorted > 0)
        {
            // determine maximum value in array
            max = 0;
            for (index = 1; index < numUnsorted; index++)
            {
                if (datos[max] < datos[index]) max = index;
            }
            int temp; 
            temp = datos[max];
            datos[max] = datos[numUnsorted-1];
            datos[numUnsorted-1] = temp;  
           // swap(datos,max,numUnsorted-1);
            numUnsorted--;
            }
    }
    
    public void setData(int[] nuevo){
        datos = nuevo;
        size = nuevo.length; 
    }
    
    public int[] getData(){
        return datos;
    }
    
    public int getPos(int n){
        if (n<=size)
        return datos[n];
        else
           return -1;
    }
}
